﻿using AutoMapper;
using MediatR;
using Traceability.Doamin;
using Traceability.ErrorCode;
using Traceability.Infratruvture.Interfares;
using Traceability.Read.Api.Application.Command.CommodityCommand;
using Traceability.Read.Api.Dto;

namespace Traceability.Read.Api.Application.CommandHander.CommodityHander
{
    public class GetCommodityNameHander : IRequestHandler<GetCommodityNameCommand, ApiResult<List<CommodityNameDto>>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<CommodityName> baseRequsitory;

        public GetCommodityNameHander(IMapper mapper, IBaseRequsitory<CommodityName> baseRequsitory)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
        }

        /// <summary>
        /// 查询商品名称处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<CommodityNameDto>>> Handle(GetCommodityNameCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<CommodityNameDto>> result = new ApiResult<List<CommodityNameDto>>();

            var list = baseRequsitory.GetAll();

            result.Code = ApiEnum.显示成功;
            result.Msg = ApiEnum.显示成功.ToString();
            result.Data = mapper.Map<List<CommodityNameDto>>(list);

            return Task.FromResult(result);
        }
    }
}
